package yhao.infra.feature.dubbo.api;

import java.io.Serializable;

/**
 * 远程调用返回封装类
 * Created by yoara on 2017/11/27.
 */
public class DubboResult<T> implements Serializable {
    private boolean success = true;
    private T body;
    private String errMessage;
    private String errCode;

    public DubboResult() {

    }

    public DubboResult(T body) {
        this.body = body;
    }

    public static <T> DubboResult ok(T body){
        return new DubboResult(body);
    }

    public DubboResult(boolean success, T body) {
        this.success = success;
        this.body = body;
    }

    public DubboResult(boolean success, T body, String errMessage) {
        this.success = success;
        this.body = body;
        this.errMessage = errMessage;
    }

    public DubboResult(boolean success, T body, String errMessage, String errCode) {
        this.success = success;
        this.body = body;
        this.errMessage = errMessage;
        this.errCode = errCode;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T pickBody() {
        if(!success){
            if(errCode!=null){
                throw new DubboResultException(errMessage,errCode);
            }
            throw new DubboResultException(errMessage);
        }
        return body;
    }

    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    public String getErrMessage() {
        return errMessage;
    }

    public void setErrMessage(String errMessage) {
        this.errMessage = errMessage;
    }

    public String getErrCode() {
        return errCode;
    }

    public void setErrCode(String errCode) {
        this.errCode = errCode;
    }
}
